package com.zhang.teachingcommunity_spring.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.teachingcommunity_spring.controller.resultType.UserPage;

import com.zhang.teachingcommunity_spring.mapper.RoleMapper;
import com.zhang.teachingcommunity_spring.mapper.SchoolMapper;
import com.zhang.teachingcommunity_spring.mapper.ScoreMapper;
import com.zhang.teachingcommunity_spring.mapper.entity.*;
import com.zhang.teachingcommunity_spring.mapper.UserMapper;
import com.zhang.teachingcommunity_spring.service.SchoolService;
import com.zhang.teachingcommunity_spring.service.UploadService;
import com.zhang.teachingcommunity_spring.service.UserService;
import com.zhang.teachingcommunity_spring.utils.Result;
import com.zhang.teachingcommunity_spring.utils.TokenUtlis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService  userService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UploadService uploadService;
    @GetMapping
    public List<User> index(){
        List<User> all = userMapper.findAll();
        return all;
    }
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        int i = userMapper.updateById(user);
        if(i == 1) {
            return  Result.success("保存成功",true);
        }
        return  Result.success("保存失败",false);
    }
    @GetMapping("/getById")
    public Result getById( Integer id){
        User user = userMapper.selectById(id);
        Integer schoolId = user.getSchoolId();
        School schools = schoolMapper.selectById(schoolId);
        user.setSchool(schools.getSchoolName());
        if(user == null) {
            return  Result.success("用户不存在", null);
        }
        return  Result.success("获取成功",user);
    }
    @PostMapping("/register")
    public Result save(@RequestBody User user){
        String saveStr = userService.save(user);
        return  Result.success(saveStr,true);
    }
    @GetMapping("/search")
    public Result search(Integer stuNo){
        QueryWrapper<User> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.like("stu_no", stuNo);
        List<User> users = userMapper.selectList(postQueryWrapper);
        return  Result.success("获取成功",users);
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        List<User> login = userService.login(user);
        if(login.size() != 0) {
            String token = TokenUtlis.genToken(login.get(0).getUserId().toString(), login.get(0).getUsername());
            login.get(0).setToken(token);
            School school = schoolService.getSchoolById(login.get(0).getSchoolId());
            login.get(0).setSchool(school.getSchoolName());
            return Result.success("登陆成功",login);
        }
        return Result.success("登录失败!用户名密码错误！", null);
    }
    @PostMapping("/findAll")
    public Result findAll(@RequestBody UserPage userPage){
        IPage<User> all = userService.findAll(userPage.getUser(),userPage.getPage());
        return Result.success("获取成功！", all);
    }
    @GetMapping("/roles")
    public Result roles(){
        QueryWrapper<Roles> objectQueryWrapper = new QueryWrapper<>();
        List<Roles> roles = roleMapper.selectList(objectQueryWrapper);
        return Result.success("获取成功！", roles);
    }
    @GetMapping("/deleteById")
    public Result deleteById(@RequestParam("id") Integer id){
        userService.deleteById(id);
        return Result.success("删除成功！", null);
    }
    @GetMapping("/searchUserName")
    public Result searchUserName( String username){
        List<User> users = userService.searchUserName(username);
        return Result.success("删除成功！", users);
    }
    @GetMapping("/registerCount")
    public Result registerCount(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_type", 1);
        List<User> teacher = userMapper.selectList(userQueryWrapper);
        QueryWrapper<User> userQueryWrapper1 = new QueryWrapper<>();
        userQueryWrapper1.eq("user_type", 0);
        List<User> student = userMapper.selectList(userQueryWrapper1);
        Map<String, Integer> stringMap = new HashMap<>();
        stringMap.put("teacher",teacher.size());
        stringMap.put("student",student.size());
        return Result.success("删除成功！", stringMap);
    }
    @GetMapping("/getDifferentSchoolCount")
    private Result getDifferentSchoolCount( ) {
        List<Map<String, Object>> differentSchoolCount = userMapper.getDifferentSchoolCount();
        return Result.success("获取成功",differentSchoolCount);
    }
    @PostMapping("/uploadAvatar")
    private Result uploadAvatar(@RequestParam("file") MultipartFile multipartFile,@RequestParam("userId") Integer userId) throws IOException {
        if(multipartFile.isEmpty()) {
            return Result.error("上传失败！", null);
        }
        String s = uploadService.uploadImg(multipartFile);
        User user = new User();
        user.setAvatar(s);
        user.setUserId(userId);
        int i = userMapper.updateById(user);
        if(i!=1) {
            return Result.success("上传失败",null);
        }
        return Result.success("上传成功",s);
    }
}
